{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-06-11T06:25:31.706308Z",
     "start_time": "2025-06-11T06:25:31.701169Z"
    }
   },
   "source": [
    "# 导包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "import lightgbm as lgb\n",
    "from sklearn.metrics import mean_squared_error, mean_absolute_error, log_loss, accuracy_score\n",
    "from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer\n",
    "from scipy import sparse\n",
    "from lightgbm.sklearn import LGBMClassifier\n",
    "from scipy.stats import kurtosis\n",
    "import time\n",
    "import warnings\n",
    "import gc\n",
    "\n",
    "warnings.filterwarnings('ignore')\n",
    "pd.set_option('display.max_columns', None)"
   ],
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:25:43.978458Z",
     "start_time": "2025-06-11T06:25:33.769825Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 重新加载数据\n",
    "# HDF5 适合存储大型矩阵数据，Parquet 适合存储列式数据\n",
    "train_values = pd.read_hdf('result_data/litetrain6' + '.h5')\n",
    "test_values = pd.read_hdf('result_data/litetest6' + '.h5')\n",
    "gx = pd.read_csv(\"result_data/gx.csv\")\n",
    "train_num_df = pd.read_parquet(\"result_data/train_num_df.parquet\")"
   ],
   "id": "6187cd4237373d5",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:26:24.526837Z",
     "start_time": "2025-06-11T06:26:24.458665Z"
    }
   },
   "cell_type": "code",
   "source": [
    "amt_labels = pd.read_csv(\"result_data/amt_labels.csv\")\n",
    "train_due_amt_df = amt_labels['array'].values\n",
    "train_due_amt_df = pd.DataFrame(train_due_amt_df)\n",
    "train_due_amt_df"
   ],
   "id": "45954248882bbccd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                 0\n",
       "0         0.000000\n",
       "1         0.000000\n",
       "2         0.000000\n",
       "3         0.000000\n",
       "4         0.000000\n",
       "...            ...\n",
       "811313    0.000000\n",
       "811314    0.000000\n",
       "811315    0.000000\n",
       "811316  180.969498\n",
       "811317    0.000000\n",
       "\n",
       "[811318 rows x 1 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811313</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811314</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811315</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811316</th>\n",
       "      <td>180.969498</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811317</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>811318 rows × 1 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:26:27.164163Z",
     "start_time": "2025-06-11T06:26:27.075613Z"
    }
   },
   "cell_type": "code",
   "source": [
    "clf_labels_df = pd.read_csv(\"result_data/clf_labels.csv\")\n",
    "clf_labels_arr = clf_labels_df['array'].values\n",
    "arr = np.array(clf_labels_arr)\n",
    "clf_labels = np.array(arr.tolist())\n",
    "clf_labels"
   ],
   "id": "7314db0ae2b92674",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, ..., 0, 1, 0])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:28:53.245280Z",
     "start_time": "2025-06-11T06:28:53.182893Z"
    }
   },
   "cell_type": "code",
   "source": [
    "amt_labels_df = pd.read_csv(\"result_data/amt_labels.csv\")\n",
    "amt_labels = amt_labels_df.values.flatten()\n",
    "amt_labels"
   ],
   "id": "adf8956ae98f1cd6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0.        ,   0.        ,   0.        , ...,   0.        ,\n",
       "       180.96949768,   0.        ])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:26:31.109193Z",
     "start_time": "2025-06-11T06:26:31.105096Z"
    }
   },
   "cell_type": "code",
   "source": "print(type(amt_labels))",
   "id": "97e4fca084ee13c5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:26:31.663636Z",
     "start_time": "2025-06-11T06:26:31.659613Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 删除不需要的特征\n",
    "for i in train_values.keys().copy():\n",
    "    if 'last0dayofweek' in i and 'cod' in i or i in ['usern', 'usernj']:\n",
    "        train_values.pop(i)\n",
    "        test_values.pop(i)"
   ],
   "id": "643f14b5577e6438",
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:26:32.943482Z",
     "start_time": "2025-06-11T06:26:32.938032Z"
    }
   },
   "cell_type": "code",
   "source": [
    "train_num_arr = np.array(train_num_df['number'].values)\n",
    "train_num = train_num_arr[0]\n",
    "train_num"
   ],
   "id": "6afb3feba5ec45e8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "811318"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:26:34.417553Z",
     "start_time": "2025-06-11T06:26:34.332697Z"
    }
   },
   "cell_type": "code",
   "source": [
    "train_due_amt_df = pd.read_csv(\"result_data/train_due_amt.csv\")\n",
    "train_due_amt_df"
   ],
   "id": "a3a240b7331ae24b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         due_amt\n",
       "0       506.0119\n",
       "1       506.0119\n",
       "2       506.0119\n",
       "3       506.0119\n",
       "4       506.0119\n",
       "...          ...\n",
       "811313  180.9695\n",
       "811314  180.9695\n",
       "811315  180.9695\n",
       "811316  180.9695\n",
       "811317  180.9695\n",
       "\n",
       "[811318 rows x 1 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>due_amt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>506.0119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>506.0119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>506.0119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>506.0119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>506.0119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811313</th>\n",
       "      <td>180.9695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811314</th>\n",
       "      <td>180.9695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811315</th>\n",
       "      <td>180.9695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811316</th>\n",
       "      <td>180.9695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>811317</th>\n",
       "      <td>180.9695</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>811318 rows × 1 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:26:36.091725Z",
     "start_time": "2025-06-11T06:26:36.087057Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#准备分组信息\n",
    "gx1, gx2 = gx[:train_num], gx[train_num:]\n",
    "classes = 1\n",
    "print(train_values.shape)"
   ],
   "id": "70616e2b5a9ddec3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(811318, 237)\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:26:37.492616Z",
     "start_time": "2025-06-11T06:26:37.488159Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用分层K折交叉验证进行模型训练\n",
    "skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=2019)\n",
    "# 初始化预测结果数组\n",
    "amt_oof = np.zeros(train_num)\n",
    "prob_oof = np.zeros((train_num, classes))\n",
    "test_pred_prob = np.zeros((test_values.shape[0],))"
   ],
   "id": "8dd0c6dc8ca3a80",
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:35:21.503038Z",
     "start_time": "2025-06-11T06:33:11.952765Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 开始交叉验证\n",
    "for i, (trn_idx, val_idx) in enumerate(skf.split(train_values, clf_labels)): \n",
    "    print(i, 'fold...')\n",
    "    t = time.time()\n",
    "    \n",
    "    # 在循环开始处添加类型转换\n",
    "    trn_idx = trn_idx.astype(int)\n",
    "    val_idx = val_idx.astype(int)\n",
    "    \n",
    "    # 准备验证集数据\n",
    "    # val_repay_amt = amt_labels[val_idx]\n",
    "    val_repay_amt = amt_labels[val_idx].flatten()\n",
    "    val_due_amt = train_due_amt_df.iloc[val_idx]\n",
    "    valgx = gx1.iloc[val_idx]\n",
    "    \n",
    "    # 创建LightGBM数据集\n",
    "    lgb_train = lgb.Dataset(train_values.iloc[trn_idx], clf_labels[trn_idx], free_raw_data=False)\n",
    "    lgb_eval = lgb.Dataset(train_values.iloc[val_idx], clf_labels[val_idx])\n",
    "\n",
    "    # LightGBM参数设置（通过贝叶斯优化找到的参数）\n",
    "    params = {\n",
    "        # 提升类型 (gbdt, dart, goss, rf)\n",
    "        'boosting_type': 'gbdt',\n",
    "        # 目标函数 (regression, binary, multiclass等)\n",
    "        'objective': 'binary',\n",
    "        # 学习率\n",
    "        'learning_rate': 0.05,\n",
    "        # 特征采样比例\n",
    "        'feature_fraction': 0.9,\n",
    "        # 数据采样比例\n",
    "        'bagging_fraction': 0.9,\n",
    "        # 随机数种子，设置为0表示使用固定种子，确保结果可复现\n",
    "        'bagging_seed': 0,\n",
    "        # 每n次迭代执行一次bagging（子采样）\n",
    "        'bagging_freq': 1,\n",
    "        # 输出日志的详细程度（0：不输出，1：输出关键信息，>1：更详细的调试信息）\n",
    "        'verbose': 1,\n",
    "        # l1正则化的权重\n",
    "        'reg_alpha': 1,\n",
    "        # l2正则化的权重\n",
    "        'reg_lambda': 2,\n",
    "        # 样本采样比例\n",
    "        'subsample': 0.8,\n",
    "        # 子样本的采用频率\n",
    "        'subsample_freq': 1,\n",
    "        # 特征采样比例\n",
    "        'colsample_bytree': 0.8,\n",
    "        # 随机数种子\n",
    "        'random_state': 2019\n",
    "    }\n",
    "\n",
    "    # gbm = lgb.train(params,lgb_train,num_boost_round=30000,verbose_eval=20,valid_sets=lgb_eval,early_stopping_rounds=100)\n",
    "    from lightgbm import log_evaluation, early_stopping\n",
    "\n",
    "    # 训练模型\n",
    "    gbm = lgb.train(\n",
    "        params,\n",
    "        lgb_train,\n",
    "        num_boost_round=30000,\n",
    "        valid_sets=lgb_eval,\n",
    "        # 每20轮打印一次日志\n",
    "        callbacks=[log_evaluation(20), early_stopping(100)]  \n",
    "    )\n",
    "    \n",
    "    '''\n",
    "    a=gbm.predict(train_values,pred_leaf=True, num_iteration=gbm.best_iteration)\n",
    "    b=gbm.predict(test_values,pred_leaf=True, num_iteration=gbm.best_iteration)\n",
    "    c=pd.DataFrame()\n",
    "    d=pd.DataFrame()\n",
    "    for ix in range(a.shape[1]):\n",
    "        c['f'+str(ix)]=a[:,ix]\n",
    "        d['f'+str(ix)]=b[:,ix]\n",
    "    c.to_hdf('trainl'+str(i)+'.h5', key='df', mode='w')\n",
    "    d.to_hdf('testl'+str(i)+'.h5', key='df', mode='w')\n",
    "    del c,d\n",
    "    '''\n",
    "\n",
    "    # 在验证集上进行预测\n",
    "    val_pred_prob_everyday = gbm.predict(train_values.iloc[val_idx], num_iteration=gbm.best_iteration)\n",
    "\n",
    "    # 处理分组预测结果\n",
    "    valgx['p'] = val_pred_prob_everyday\n",
    "    gg = valgx.groupby(['user_id', 'listing_id'])['p'].sum().reset_index(name='av')\n",
    "    valgx = valgx.merge(gg, on=['user_id', 'listing_id'], how='left')\n",
    "\n",
    "    # 计算预测还款金额\n",
    "    val_pred_repay_amt = val_due_amt['due_amt'].values * (val_pred_prob_everyday / valgx['av'].values)\n",
    "\n",
    "    # 打印验证集评估指标\n",
    "    print('val rmse:', np.sqrt(mean_squared_error(val_repay_amt, val_pred_repay_amt)))\n",
    "    print('val mae:', mean_absolute_error(val_repay_amt, val_pred_repay_amt))\n",
    "\n",
    "    # 保存预测结果\n",
    "    amt_oof[val_idx] = val_pred_repay_amt\n",
    "    test_pred_prob += gbm.predict(test_values, num_iteration=gbm.best_iteration) / skf.n_splits\n",
    "\n",
    "    print('runtime: {}\\n'.format(time.time() - t))"
   ],
   "id": "86c047d3ccd97b25",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 fold...\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Info] Number of positive: 19998, number of negative: 629056\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.337364 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 28764\n",
      "[LightGBM] [Info] Number of data points in the train set: 649054, number of used features: 237\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.030811 -> initscore=-3.448588\n",
      "[LightGBM] [Info] Start training from score -3.448588\n",
      "Training until validation scores don't improve for 100 rounds\n",
      "[20]\tvalid_0's binary_logloss: 0.0965789\n",
      "[40]\tvalid_0's binary_logloss: 0.0917163\n",
      "[60]\tvalid_0's binary_logloss: 0.0905119\n",
      "[80]\tvalid_0's binary_logloss: 0.0900875\n",
      "[100]\tvalid_0's binary_logloss: 0.0899587\n",
      "[120]\tvalid_0's binary_logloss: 0.0899097\n",
      "[140]\tvalid_0's binary_logloss: 0.089906\n",
      "[160]\tvalid_0's binary_logloss: 0.089904\n",
      "[180]\tvalid_0's binary_logloss: 0.0898961\n",
      "[200]\tvalid_0's binary_logloss: 0.089906\n",
      "[220]\tvalid_0's binary_logloss: 0.0899013\n",
      "[240]\tvalid_0's binary_logloss: 0.0898973\n",
      "[260]\tvalid_0's binary_logloss: 0.0899188\n",
      "[280]\tvalid_0's binary_logloss: 0.0899161\n",
      "[300]\tvalid_0's binary_logloss: 0.0899186\n",
      "Early stopping, best iteration is:\n",
      "[212]\tvalid_0's binary_logloss: 0.0898872\n",
      "val rmse: 173.3224115604391\n",
      "val mae: 66.35698581958844\n",
      "runtime: 27.358468055725098\n",
      "\n",
      "1 fold...\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Info] Number of positive: 19997, number of negative: 629057\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.304338 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 28769\n",
      "[LightGBM] [Info] Number of data points in the train set: 649054, number of used features: 237\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.030809 -> initscore=-3.448640\n",
      "[LightGBM] [Info] Start training from score -3.448640\n",
      "Training until validation scores don't improve for 100 rounds\n",
      "[20]\tvalid_0's binary_logloss: 0.0972257\n",
      "[40]\tvalid_0's binary_logloss: 0.0923543\n",
      "[60]\tvalid_0's binary_logloss: 0.0910449\n",
      "[80]\tvalid_0's binary_logloss: 0.0906991\n",
      "[100]\tvalid_0's binary_logloss: 0.0905122\n",
      "[120]\tvalid_0's binary_logloss: 0.0904558\n",
      "[140]\tvalid_0's binary_logloss: 0.0904247\n",
      "[160]\tvalid_0's binary_logloss: 0.0903931\n",
      "[180]\tvalid_0's binary_logloss: 0.0904092\n",
      "[200]\tvalid_0's binary_logloss: 0.0904149\n",
      "[220]\tvalid_0's binary_logloss: 0.0904244\n",
      "[240]\tvalid_0's binary_logloss: 0.0904187\n",
      "[260]\tvalid_0's binary_logloss: 0.0904302\n",
      "Early stopping, best iteration is:\n",
      "[160]\tvalid_0's binary_logloss: 0.0903931\n",
      "val rmse: 174.32518454751087\n",
      "val mae: 66.4472063382456\n",
      "runtime: 23.155268907546997\n",
      "\n",
      "2 fold...\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Info] Number of positive: 19997, number of negative: 629057\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.304070 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 28770\n",
      "[LightGBM] [Info] Number of data points in the train set: 649054, number of used features: 237\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.030809 -> initscore=-3.448640\n",
      "[LightGBM] [Info] Start training from score -3.448640\n",
      "Training until validation scores don't improve for 100 rounds\n",
      "[20]\tvalid_0's binary_logloss: 0.0961639\n",
      "[40]\tvalid_0's binary_logloss: 0.0912023\n",
      "[60]\tvalid_0's binary_logloss: 0.0899116\n",
      "[80]\tvalid_0's binary_logloss: 0.0895584\n",
      "[100]\tvalid_0's binary_logloss: 0.0893967\n",
      "[120]\tvalid_0's binary_logloss: 0.0893523\n",
      "[140]\tvalid_0's binary_logloss: 0.0893582\n",
      "[160]\tvalid_0's binary_logloss: 0.0893407\n",
      "[180]\tvalid_0's binary_logloss: 0.0893545\n",
      "[200]\tvalid_0's binary_logloss: 0.0893501\n",
      "[220]\tvalid_0's binary_logloss: 0.0893388\n",
      "[240]\tvalid_0's binary_logloss: 0.0893212\n",
      "[260]\tvalid_0's binary_logloss: 0.089317\n",
      "[280]\tvalid_0's binary_logloss: 0.0893207\n",
      "[300]\tvalid_0's binary_logloss: 0.0893354\n",
      "[320]\tvalid_0's binary_logloss: 0.089338\n",
      "[340]\tvalid_0's binary_logloss: 0.0893255\n",
      "[360]\tvalid_0's binary_logloss: 0.0893421\n",
      "Early stopping, best iteration is:\n",
      "[263]\tvalid_0's binary_logloss: 0.0893092\n",
      "val rmse: 176.75085528665912\n",
      "val mae: 66.5175684420632\n",
      "runtime: 28.218076705932617\n",
      "\n",
      "3 fold...\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Info] Number of positive: 19998, number of negative: 629057\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.316923 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 28774\n",
      "[LightGBM] [Info] Number of data points in the train set: 649055, number of used features: 237\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.030811 -> initscore=-3.448590\n",
      "[LightGBM] [Info] Start training from score -3.448590\n",
      "Training until validation scores don't improve for 100 rounds\n",
      "[20]\tvalid_0's binary_logloss: 0.0958353\n",
      "[40]\tvalid_0's binary_logloss: 0.0908957\n",
      "[60]\tvalid_0's binary_logloss: 0.0896159\n",
      "[80]\tvalid_0's binary_logloss: 0.0892902\n",
      "[100]\tvalid_0's binary_logloss: 0.0891664\n",
      "[120]\tvalid_0's binary_logloss: 0.0891023\n",
      "[140]\tvalid_0's binary_logloss: 0.089047\n",
      "[160]\tvalid_0's binary_logloss: 0.0890066\n",
      "[180]\tvalid_0's binary_logloss: 0.0889887\n",
      "[200]\tvalid_0's binary_logloss: 0.0889719\n",
      "[220]\tvalid_0's binary_logloss: 0.089006\n",
      "[240]\tvalid_0's binary_logloss: 0.0890108\n",
      "[260]\tvalid_0's binary_logloss: 0.0889917\n",
      "[280]\tvalid_0's binary_logloss: 0.0890017\n",
      "Early stopping, best iteration is:\n",
      "[198]\tvalid_0's binary_logloss: 0.0889678\n",
      "val rmse: 169.30298170977608\n",
      "val mae: 66.15177452269344\n",
      "runtime: 24.261045932769775\n",
      "\n",
      "4 fold...\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Info] Number of positive: 19998, number of negative: 629057\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.303306 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 28789\n",
      "[LightGBM] [Info] Number of data points in the train set: 649055, number of used features: 237\n",
      "[LightGBM] [Warning] feature_fraction is set=0.9, colsample_bytree=0.8 will be ignored. Current value: feature_fraction=0.9\n",
      "[LightGBM] [Warning] bagging_freq is set=1, subsample_freq=1 will be ignored. Current value: bagging_freq=1\n",
      "[LightGBM] [Warning] bagging_fraction is set=0.9, subsample=0.8 will be ignored. Current value: bagging_fraction=0.9\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.030811 -> initscore=-3.448590\n",
      "[LightGBM] [Info] Start training from score -3.448590\n",
      "Training until validation scores don't improve for 100 rounds\n",
      "[20]\tvalid_0's binary_logloss: 0.0964356\n",
      "[40]\tvalid_0's binary_logloss: 0.0915031\n",
      "[60]\tvalid_0's binary_logloss: 0.0902446\n",
      "[80]\tvalid_0's binary_logloss: 0.0898658\n",
      "[100]\tvalid_0's binary_logloss: 0.0896733\n",
      "[120]\tvalid_0's binary_logloss: 0.0896408\n",
      "[140]\tvalid_0's binary_logloss: 0.0895999\n",
      "[160]\tvalid_0's binary_logloss: 0.0895707\n",
      "[180]\tvalid_0's binary_logloss: 0.0895323\n",
      "[200]\tvalid_0's binary_logloss: 0.089538\n",
      "[220]\tvalid_0's binary_logloss: 0.0895351\n",
      "[240]\tvalid_0's binary_logloss: 0.0895346\n",
      "[260]\tvalid_0's binary_logloss: 0.0895314\n",
      "[280]\tvalid_0's binary_logloss: 0.0895425\n",
      "[300]\tvalid_0's binary_logloss: 0.0895549\n",
      "[320]\tvalid_0's binary_logloss: 0.0895639\n",
      "Early stopping, best iteration is:\n",
      "[231]\tvalid_0's binary_logloss: 0.0895177\n",
      "val rmse: 172.85621330748444\n",
      "val mae: 66.11484153872746\n",
      "runtime: 26.438639640808105\n",
      "\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:36:46.515851Z",
     "start_time": "2025-06-11T06:36:46.399270Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import gc\n",
    "\n",
    "# 计算特征重要性\n",
    "fold_importance_df = pd.DataFrame()\n",
    "fold_importance_df[\"feature\"] = gbm.feature_name()\n",
    "fold_importance_df[\"importance\"] = gbm.feature_importance(importance_type='gain')\n",
    "gc.collect()"
   ],
   "id": "97d44c8d9af0aceb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3269"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:38:03.771317Z",
     "start_time": "2025-06-11T06:38:03.706219Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 打印交叉验证结果\n",
    "print('\\ncv rmse:', np.sqrt(mean_squared_error(amt_labels, amt_oof)))\n",
    "print('cv mae:', mean_absolute_error(amt_labels, amt_oof))\n",
    "print('cv acc:', accuracy_score(clf_labels, np.argmax(prob_oof, axis=1)))"
   ],
   "id": "f81340c581a1a32c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "cv rmse: 173.32834318895007\n",
      "cv mae: 66.31767578675202\n",
      "cv acc: 0.9691896395741251\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:38:12.323283Z",
     "start_time": "2025-06-11T06:38:11.873072Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 处理测试集预测结果\n",
    "gx2['p'] = test_pred_prob\n",
    "gg = gx2.groupby('listing_id')['p'].sum().reset_index(name='av')\n",
    "gx2 = gx2.merge(gg, on='listing_id', how='left')\n",
    "gx2['pv'] = gx2['p'] / gx2['av']"
   ],
   "id": "79921fd32edb6044",
   "outputs": [],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:38:21.746892Z",
     "start_time": "2025-06-11T06:38:18.269737Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 准备提交文件\n",
    "sub_example = pd.read_csv('data/submission.csv', parse_dates=['repay_date'])\n",
    "test_df = pd.read_csv('data/test.csv', parse_dates=['auditing_date', 'due_date'])\n",
    "sub = test_df[['listing_id', 'auditing_date', 'due_date', 'due_amt']]\n",
    "sub_example = sub_example.merge(sub, on='listing_id', how='left')\n",
    "sub_example['repay_date'] = pd.to_datetime(sub_example['repay_date'])\n",
    "gx2['repay_date'] = pd.to_datetime(gx2['repay_date'])\n",
    "sub_example = sub_example.merge(gx2, on=['repay_date', 'listing_id'], how='left')\n",
    "gc.collect()"
   ],
   "id": "6028033d856ab455",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:38:38.989405Z",
     "start_time": "2025-06-11T06:38:38.977503Z"
    }
   },
   "cell_type": "code",
   "source": "sub_example",
   "id": "be47bd27c09b0b4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         listing_id  repay_amt repay_date auditing_date   due_date   due_amt  \\\n",
       "0           5431438     4.3309 2019-03-12    2019-03-12 2019-04-12  138.5903   \n",
       "1           5431438     4.3309 2019-03-13    2019-03-12 2019-04-12  138.5903   \n",
       "2           5431438     4.3309 2019-03-14    2019-03-12 2019-04-12  138.5903   \n",
       "3           5431438     4.3309 2019-03-15    2019-03-12 2019-04-12  138.5903   \n",
       "4           5431438     4.3309 2019-03-16    2019-03-12 2019-04-12  138.5903   \n",
       "...             ...        ...        ...           ...        ...       ...   \n",
       "3987073     5460170     9.1821 2019-04-17    2019-03-21 2019-04-21  293.8277   \n",
       "3987074     5460170     9.1821 2019-04-18    2019-03-21 2019-04-21  293.8277   \n",
       "3987075     5460170     9.1821 2019-04-19    2019-03-21 2019-04-21  293.8277   \n",
       "3987076     5460170     9.1821 2019-04-20    2019-03-21 2019-04-21  293.8277   \n",
       "3987077     5460170     9.1821 2019-04-21    2019-03-21 2019-04-21  293.8277   \n",
       "\n",
       "         user_id         p        av        pv  \n",
       "0         498765  0.002901  0.996711  0.002911  \n",
       "1         498765  0.001486  0.996711  0.001491  \n",
       "2         498765  0.000942  0.996711  0.000945  \n",
       "3         498765  0.000651  0.996711  0.000653  \n",
       "4         498765  0.000653  0.996711  0.000655  \n",
       "...          ...       ...       ...       ...  \n",
       "3987073   265473  0.014452  1.040866  0.013884  \n",
       "3987074   265473  0.026067  1.040866  0.025044  \n",
       "3987075   265473  0.023593  1.040866  0.022667  \n",
       "3987076   265473  0.069429  1.040866  0.066703  \n",
       "3987077   265473  0.556030  1.040866  0.534199  \n",
       "\n",
       "[3987078 rows x 10 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>listing_id</th>\n",
       "      <th>repay_amt</th>\n",
       "      <th>repay_date</th>\n",
       "      <th>auditing_date</th>\n",
       "      <th>due_date</th>\n",
       "      <th>due_amt</th>\n",
       "      <th>user_id</th>\n",
       "      <th>p</th>\n",
       "      <th>av</th>\n",
       "      <th>pv</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5431438</td>\n",
       "      <td>4.3309</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.002901</td>\n",
       "      <td>0.996711</td>\n",
       "      <td>0.002911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5431438</td>\n",
       "      <td>4.3309</td>\n",
       "      <td>2019-03-13</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.001486</td>\n",
       "      <td>0.996711</td>\n",
       "      <td>0.001491</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5431438</td>\n",
       "      <td>4.3309</td>\n",
       "      <td>2019-03-14</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.000942</td>\n",
       "      <td>0.996711</td>\n",
       "      <td>0.000945</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5431438</td>\n",
       "      <td>4.3309</td>\n",
       "      <td>2019-03-15</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.000651</td>\n",
       "      <td>0.996711</td>\n",
       "      <td>0.000653</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5431438</td>\n",
       "      <td>4.3309</td>\n",
       "      <td>2019-03-16</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.000653</td>\n",
       "      <td>0.996711</td>\n",
       "      <td>0.000655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987073</th>\n",
       "      <td>5460170</td>\n",
       "      <td>9.1821</td>\n",
       "      <td>2019-04-17</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.014452</td>\n",
       "      <td>1.040866</td>\n",
       "      <td>0.013884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987074</th>\n",
       "      <td>5460170</td>\n",
       "      <td>9.1821</td>\n",
       "      <td>2019-04-18</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.026067</td>\n",
       "      <td>1.040866</td>\n",
       "      <td>0.025044</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987075</th>\n",
       "      <td>5460170</td>\n",
       "      <td>9.1821</td>\n",
       "      <td>2019-04-19</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.023593</td>\n",
       "      <td>1.040866</td>\n",
       "      <td>0.022667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987076</th>\n",
       "      <td>5460170</td>\n",
       "      <td>9.1821</td>\n",
       "      <td>2019-04-20</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.069429</td>\n",
       "      <td>1.040866</td>\n",
       "      <td>0.066703</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987077</th>\n",
       "      <td>5460170</td>\n",
       "      <td>9.1821</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.556030</td>\n",
       "      <td>1.040866</td>\n",
       "      <td>0.534199</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3987078 rows × 10 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:44:49.714700Z",
     "start_time": "2025-06-11T06:44:49.690843Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算最终还款金额\n",
    "sub_example['repay_amt'] = sub_example['due_amt'] * sub_example['pv']\n",
    "sub_example"
   ],
   "id": "aff613e79d60740e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         listing_id   repay_amt repay_date auditing_date   due_date   due_amt  \\\n",
       "0           5431438    0.403405 2019-03-12    2019-03-12 2019-04-12  138.5903   \n",
       "1           5431438    0.206570 2019-03-13    2019-03-12 2019-04-12  138.5903   \n",
       "2           5431438    0.130939 2019-03-14    2019-03-12 2019-04-12  138.5903   \n",
       "3           5431438    0.090554 2019-03-15    2019-03-12 2019-04-12  138.5903   \n",
       "4           5431438    0.090830 2019-03-16    2019-03-12 2019-04-12  138.5903   \n",
       "...             ...         ...        ...           ...        ...       ...   \n",
       "3987073     5460170    4.079566 2019-04-17    2019-03-21 2019-04-21  293.8277   \n",
       "3987074     5460170    7.358524 2019-04-18    2019-03-21 2019-04-21  293.8277   \n",
       "3987075     5460170    6.660217 2019-04-19    2019-03-21 2019-04-21  293.8277   \n",
       "3987076     5460170   19.599149 2019-04-20    2019-03-21 2019-04-21  293.8277   \n",
       "3987077     5460170  156.962581 2019-04-21    2019-03-21 2019-04-21  293.8277   \n",
       "\n",
       "         user_id         p        av        pv  \n",
       "0         498765  0.002901  0.996711  0.002911  \n",
       "1         498765  0.001486  0.996711  0.001491  \n",
       "2         498765  0.000942  0.996711  0.000945  \n",
       "3         498765  0.000651  0.996711  0.000653  \n",
       "4         498765  0.000653  0.996711  0.000655  \n",
       "...          ...       ...       ...       ...  \n",
       "3987073   265473  0.014452  1.040866  0.013884  \n",
       "3987074   265473  0.026067  1.040866  0.025044  \n",
       "3987075   265473  0.023593  1.040866  0.022667  \n",
       "3987076   265473  0.069429  1.040866  0.066703  \n",
       "3987077   265473  0.556030  1.040866  0.534199  \n",
       "\n",
       "[3987078 rows x 10 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>listing_id</th>\n",
       "      <th>repay_amt</th>\n",
       "      <th>repay_date</th>\n",
       "      <th>auditing_date</th>\n",
       "      <th>due_date</th>\n",
       "      <th>due_amt</th>\n",
       "      <th>user_id</th>\n",
       "      <th>p</th>\n",
       "      <th>av</th>\n",
       "      <th>pv</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5431438</td>\n",
       "      <td>0.403405</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.002901</td>\n",
       "      <td>0.996711</td>\n",
       "      <td>0.002911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5431438</td>\n",
       "      <td>0.206570</td>\n",
       "      <td>2019-03-13</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.001486</td>\n",
       "      <td>0.996711</td>\n",
       "      <td>0.001491</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5431438</td>\n",
       "      <td>0.130939</td>\n",
       "      <td>2019-03-14</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.000942</td>\n",
       "      <td>0.996711</td>\n",
       "      <td>0.000945</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5431438</td>\n",
       "      <td>0.090554</td>\n",
       "      <td>2019-03-15</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.000651</td>\n",
       "      <td>0.996711</td>\n",
       "      <td>0.000653</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5431438</td>\n",
       "      <td>0.090830</td>\n",
       "      <td>2019-03-16</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>2019-04-12</td>\n",
       "      <td>138.5903</td>\n",
       "      <td>498765</td>\n",
       "      <td>0.000653</td>\n",
       "      <td>0.996711</td>\n",
       "      <td>0.000655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987073</th>\n",
       "      <td>5460170</td>\n",
       "      <td>4.079566</td>\n",
       "      <td>2019-04-17</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.014452</td>\n",
       "      <td>1.040866</td>\n",
       "      <td>0.013884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987074</th>\n",
       "      <td>5460170</td>\n",
       "      <td>7.358524</td>\n",
       "      <td>2019-04-18</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.026067</td>\n",
       "      <td>1.040866</td>\n",
       "      <td>0.025044</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987075</th>\n",
       "      <td>5460170</td>\n",
       "      <td>6.660217</td>\n",
       "      <td>2019-04-19</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.023593</td>\n",
       "      <td>1.040866</td>\n",
       "      <td>0.022667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987076</th>\n",
       "      <td>5460170</td>\n",
       "      <td>19.599149</td>\n",
       "      <td>2019-04-20</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.069429</td>\n",
       "      <td>1.040866</td>\n",
       "      <td>0.066703</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987077</th>\n",
       "      <td>5460170</td>\n",
       "      <td>156.962581</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>2019-03-21</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>293.8277</td>\n",
       "      <td>265473</td>\n",
       "      <td>0.556030</td>\n",
       "      <td>1.040866</td>\n",
       "      <td>0.534199</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3987078 rows × 10 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:46:38.219725Z",
     "start_time": "2025-06-11T06:46:38.186304Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = sub_example[['listing_id', 'repay_date', 'repay_amt']]\n",
    "df"
   ],
   "id": "dccad9dfe6d7fb5f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         listing_id repay_date   repay_amt\n",
       "0           5431438 2019-03-12    0.403405\n",
       "1           5431438 2019-03-13    0.206570\n",
       "2           5431438 2019-03-14    0.130939\n",
       "3           5431438 2019-03-15    0.090554\n",
       "4           5431438 2019-03-16    0.090830\n",
       "...             ...        ...         ...\n",
       "3987073     5460170 2019-04-17    4.079566\n",
       "3987074     5460170 2019-04-18    7.358524\n",
       "3987075     5460170 2019-04-19    6.660217\n",
       "3987076     5460170 2019-04-20   19.599149\n",
       "3987077     5460170 2019-04-21  156.962581\n",
       "\n",
       "[3987078 rows x 3 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>listing_id</th>\n",
       "      <th>repay_date</th>\n",
       "      <th>repay_amt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5431438</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>0.403405</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5431438</td>\n",
       "      <td>2019-03-13</td>\n",
       "      <td>0.206570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5431438</td>\n",
       "      <td>2019-03-14</td>\n",
       "      <td>0.130939</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5431438</td>\n",
       "      <td>2019-03-15</td>\n",
       "      <td>0.090554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5431438</td>\n",
       "      <td>2019-03-16</td>\n",
       "      <td>0.090830</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987073</th>\n",
       "      <td>5460170</td>\n",
       "      <td>2019-04-17</td>\n",
       "      <td>4.079566</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987074</th>\n",
       "      <td>5460170</td>\n",
       "      <td>2019-04-18</td>\n",
       "      <td>7.358524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987075</th>\n",
       "      <td>5460170</td>\n",
       "      <td>2019-04-19</td>\n",
       "      <td>6.660217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987076</th>\n",
       "      <td>5460170</td>\n",
       "      <td>2019-04-20</td>\n",
       "      <td>19.599149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987077</th>\n",
       "      <td>5460170</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>156.962581</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3987078 rows × 3 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:47:15.033190Z",
     "start_time": "2025-06-11T06:47:07.889282Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 保存提交文件\n",
    "df.to_csv('result_data/re.csv', index=False)"
   ],
   "id": "7fa28bb33a6f1a34",
   "outputs": [],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-11T06:57:21.553442Z",
     "start_time": "2025-06-11T06:57:20.411191Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df2 = pd.read_csv('result_data/re.csv')\n",
    "df2"
   ],
   "id": "f8df874406857931",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         listing_id  repay_date   repay_amt\n",
       "0           5431438  2019-03-12    0.403405\n",
       "1           5431438  2019-03-13    0.206570\n",
       "2           5431438  2019-03-14    0.130939\n",
       "3           5431438  2019-03-15    0.090554\n",
       "4           5431438  2019-03-16    0.090830\n",
       "...             ...         ...         ...\n",
       "3987073     5460170  2019-04-17    4.079566\n",
       "3987074     5460170  2019-04-18    7.358524\n",
       "3987075     5460170  2019-04-19    6.660217\n",
       "3987076     5460170  2019-04-20   19.599149\n",
       "3987077     5460170  2019-04-21  156.962581\n",
       "\n",
       "[3987078 rows x 3 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>listing_id</th>\n",
       "      <th>repay_date</th>\n",
       "      <th>repay_amt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5431438</td>\n",
       "      <td>2019-03-12</td>\n",
       "      <td>0.403405</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5431438</td>\n",
       "      <td>2019-03-13</td>\n",
       "      <td>0.206570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5431438</td>\n",
       "      <td>2019-03-14</td>\n",
       "      <td>0.130939</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5431438</td>\n",
       "      <td>2019-03-15</td>\n",
       "      <td>0.090554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5431438</td>\n",
       "      <td>2019-03-16</td>\n",
       "      <td>0.090830</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987073</th>\n",
       "      <td>5460170</td>\n",
       "      <td>2019-04-17</td>\n",
       "      <td>4.079566</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987074</th>\n",
       "      <td>5460170</td>\n",
       "      <td>2019-04-18</td>\n",
       "      <td>7.358524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987075</th>\n",
       "      <td>5460170</td>\n",
       "      <td>2019-04-19</td>\n",
       "      <td>6.660217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987076</th>\n",
       "      <td>5460170</td>\n",
       "      <td>2019-04-20</td>\n",
       "      <td>19.599149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3987077</th>\n",
       "      <td>5460170</td>\n",
       "      <td>2019-04-21</td>\n",
       "      <td>156.962581</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3987078 rows × 3 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 33
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
